package com.lxc.trigger.http;

import com.alibaba.fastjson.JSON;
import com.lxc.domain.docker.model.aggregates.NginxConfig;
import com.lxc.domain.docker.model.entity.LocationEntity;
import com.lxc.domain.docker.model.entity.UpstreamEntity;
import com.lxc.domain.docker.service.ILoadBalancingService;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author lxc18
 * @date 2024/12/29 10:38
 * @description LoadBalancingManageController
 */
@RestController
@RequestMapping("/wg/admin/load")
public class LoadBalancingManageController {
    private Logger logger = LoggerFactory.getLogger(LoadBalancingManageController.class);

    @Resource
    private ILoadBalancingService loadBalancingService;

    /**
     * http://localhost:8001/wg/admin/load/copy
     */
    @GetMapping(value = "copy", produces = "application/json;charset=utf-8")
    public void copy() throws IOException {
        ProcessBuilder pb = new ProcessBuilder("docker", "cp", "/nginx.conf", "host:/Users/fuzhengwei/1024/KnowledgePlanet/api-gateway/api-gateway-center/doc/data/nginx/nginx.conf");
        pb.start();
    }

    private static void unTar(TarArchiveInputStream tis, File destFile)
            throws IOException {
        TarArchiveEntry tarEntry = null;
        while ((tarEntry = tis.getNextTarEntry()) != null) {
            if (tarEntry.isDirectory()) {
                if (!destFile.exists()) {
                    destFile.mkdirs();
                }
            } else {
                FileOutputStream fos = new FileOutputStream(destFile);
                IOUtils.copy(tis, fos);
                fos.close();
            }
        }
        tis.close();
    }

    /**
     * http://localhost:8001/wg/admin/load/updateNginxConfig
     */
    @GetMapping(value = "updateNginxConfig", produces = "application/json;charset=utf-8")
    public void updateNginxConfig() {
        List<UpstreamEntity> upstreamList = new ArrayList<>();
        upstreamList.add(new UpstreamEntity("api03", "least_conn;", Arrays.asList("newsapi.org;", "jsonplaceholder.typicode.com;")));
        upstreamList.add(new UpstreamEntity("api04", "least_conn;", Arrays.asList("api.openweathermap.org;")));

        List<LocationEntity> locationList = new ArrayList<>();
        locationList.add(new LocationEntity("/api03/", "http://api03;"));
        locationList.add(new LocationEntity("/api04/", "http://api04;"));

        String localNginxPath = "C:\\Users\\lxc18\\IdeaProjects\\api-gateway-center\\api-gateway-center\\docs\\dev-ops\\nginx\\nginx.conf";
        String remoteNginxPath = "/etc/nginx/";
        String applicationName = "api-gateway-center";
        String nginxName = "mynginx";
        NginxConfig nginxConfig = new NginxConfig(applicationName,nginxName, localNginxPath, remoteNginxPath, upstreamList, locationList);
        try {
            logger.info("刷新Nginx配置文件开始 nginxConfig：{}", JSON.toJSONString(nginxConfig));
            loadBalancingService.updateNginxConfig(nginxConfig);
            logger.info("刷新Nginx配置文件完成");
        } catch (Exception e) {
            logger.error("刷新Nginx配置文件失败", e);
        }
    }


}
